﻿Imports System.ComponentModel
Imports Global_Visas_Business_Layer
Partial Public Class frmAdminEmailAutomation

    Private Sub frmAdminEmailAutomation_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmAdminEmailAutomation_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        With drpFrom
            .DisplayMemberPath = "CaseStage_Title"
            .SelectedValuePath = "StageLink_FromID"
            .ItemsSource = CType(Admin.GetcaseStages(LblDiagramid.Content), IListSource).GetList
            .SelectedIndex = -1
        End With
        With cboEmailTemplate
            .DisplayMemberPath = "EMailTemplate_Name"
            .SelectedValuePath = "EMailTemplate_ID"
            .ItemsSource = CType(Admin.GetEmailtemplateslist(), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub

    Private Sub drpFrom_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpFrom.SelectionChanged
        If drpFrom.SelectedValue > 0 Then
            With drpTo
                .DisplayMemberPath = "CaseStage_Title"
                .SelectedValuePath = "StageLink_ToID"
                .ItemsSource = CType(Admin.GetcaseStages_To(drpFrom.SelectedValue, LblDiagramid.Content), IListSource).GetList
                .SelectedIndex = -1
            End With
        End If
        cboEmailTemplate.IsEnabled = False
        cboEmailTemplate.SelectedIndex = -1
        txtBody.Text = String.Empty
    End Sub

    Private Sub drpTo_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpTo.SelectionChanged

        If drpFrom.SelectedValue > 0 And drpTo.SelectedValue > 0 Then
            cboEmailTemplate.SelectedValue = Admin.GetEmailTemplateforcasestage(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content)
            cboEmailTemplate.IsEnabled = True
        End If
    End Sub

    Private Sub cboEmailTemplate_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboEmailTemplate.SelectionChanged
        If Not cboEmailTemplate.SelectedItem Is Nothing Then
            If cboEmailTemplate.SelectedItem.row.itemarray(0).ToString > 0 Then
                txtBody.Text = Admin.GetEmailTemplateBodyFromID(cboEmailTemplate.SelectedItem.row.itemarray(0).ToString)
            End If
        End If
        btnSave.IsEnabled = True
        btnSetNothing.IsEnabled = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Dim TasksEvents As TasksEvents
        Try

            If Admin.UpdateEmailAutomation(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content, cboEmailTemplate.SelectedValue) = True Then
                TasksEvents = New TasksEvents
                TasksEvents.GblFunc_Add_SysEvent("Email Template", "The email template has been updated as " & cboEmailTemplate.SelectedValue & " for the case stage - From : " & drpFrom.SelectedValue & " To : " & drpTo.SelectedValue & " and Diagramid : " & LblDiagramid.Content)
                CommonFuncs.DisplayMessage("The email template has been updated", CommonDialog.CustomDialogIcons.Information, "Email Template")
            End If
        Catch ex As Exception
        Finally
            TasksEvents = Nothing
        End Try
    End Sub

    Private Sub cboEmailTemplate_TargetUpdated(ByVal sender As Object, ByVal e As System.Windows.Data.DataTransferEventArgs) Handles cboEmailTemplate.TargetUpdated

    End Sub

    Private Sub btnSetNothing_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSetNothing.Click
        Dim TasksEvents As TasksEvents
        Try

            If Admin.UpdateEmailAutomation(drpFrom.SelectedValue, drpTo.SelectedValue, LblDiagramid.Content, 0) = True Then
                TasksEvents = New TasksEvents
                TasksEvents.GblFunc_Add_SysEvent("Email Template", "The email template has been updated as " & 0 & " for the case stage - From : " & drpFrom.SelectedValue & " To : " & drpTo.SelectedValue & " and Diagramid : " & LblDiagramid.Content)
                CommonFuncs.DisplayMessage("The email template has been set to nothing", CommonDialog.CustomDialogIcons.Information, "Email Template")
            End If
        Catch ex As Exception
        Finally
            TasksEvents = Nothing
        End Try
    End Sub
End Class
